package com.cn.utils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import static org.springframework.web.util.UriUtils.encodeQuery;

public class EnglishWordUtil {

    /**
     * 发送请求
     *
     * @param queryWord
     * @return
     * @throws Exception
     */
    public static JSONObject sendRequest(String queryWord) throws Exception {
        // 设置URL
        String url = "https://dict.youdao.com/jsonapi?q=" + queryWord;

        // 创建URL对象并打开连接
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // 设置请求方法为GET
        con.setRequestMethod("GET");
        con.setRequestProperty("Content-Type", "application/json");

        // 读取响应
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // 解析JSON响应
        JSONObject json = new JSONObject(response.toString());
        return json;
    }

    /**
     * 获取有道词典-单词所有的信息
     * 英式音标
     * 美式音标
     * 单词意思
     *
     * @param queryWord
     */
    public static HashMap<String, String> getWordAllMessage(String queryWord) throws Exception {
        JSONObject json = sendRequest(queryWord);
        HashMap<String, String> translateResult = new HashMap<>();

        // 获取美式和英式音标
        JSONObject word = json.getJSONObject("ec").getJSONArray("word").getJSONObject(0);
        String usphone = word.optString("usphone", "");
        String ukphone = word.optString("ukphone", "");

        //封装音标
        translateResult.put("usPhonetic","["+usphone+"]");
        translateResult.put("ukPhonetic","["+ukphone+"]");

        // 获取翻译列表
        JSONArray trsArray = word.getJSONArray("trs");
        //System.out.println(trsArray);
        StringBuilder translations = new StringBuilder();

        for (int i = 0; i < trsArray.length(); i++) {
            String translation = trsArray.getJSONObject(i).getJSONArray("tr")
                    .getJSONObject(0).getJSONObject("l").getJSONArray("i").getString(0);
            translations.append(translation).append("\n");
        }
        //封装释义
        translateResult.put("meanings",translations.toString().trim());

        //封装单词发音
        String usVoice = getUsWordVoice(queryWord);
        String ukVoice = getUkWordVoice(queryWord);

        translateResult.put("usVoice",usVoice);
        translateResult.put("ukVoice",ukVoice);

        // 输出翻译结果
        //System.out.println(translations.toString().trim());
        return translateResult;

    }

    /**
     * 获取英语单词的发音url链接
     * @return
     */
    public static HashMap<String,String> getWordVoice(String word){
        String us_voice_pattern = "https://dict.youdao.com/dictvoice?type=0&audio=";
        String uk_voice_pattern = "https://dict.youdao.com/dictvoice?type=1&audio=";
        HashMap<String, String> wordVoice = new HashMap<>();
        wordVoice.put("usVoice",us_voice_pattern+word);
        wordVoice.put("ukVoice",uk_voice_pattern+word);
        return wordVoice;
    }

    public static String getUsWordVoice(String word){
        String us_voice_pattern = "https://dict.youdao.com/dictvoice?type=0&audio=";
        return us_voice_pattern+word;
    }

    public static String getUkWordVoice(String word){
        String uk_voice_pattern = "https://dict.youdao.com/dictvoice?type=1&audio=";
        return uk_voice_pattern+word;
    }


    public static void main(String[] args) throws Exception {
        HashMap<String, String> word = getWordAllMessage("pollutants");
        System.out.println(word);
    }

}
